package com.android.WuxiBus;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class SOAPUtil
{
    public static SoapObject TransportData(String soapAction, Map<String, String> paramMap)
    {
        
        SoapObject localSoapObject2 = null;
        try
        {
            SoapObject localSoapObject = new SoapObject("http://tempuri.org/", soapAction);
            
            for (Iterator<String> it =  paramMap.keySet().iterator(); it.hasNext(); )   
            { 
                String key = (String)it.next();
                localSoapObject.addProperty(key, paramMap.get(key));
            }
            
            SoapSerializationEnvelope localSoapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            localSoapSerializationEnvelope.bodyOut = localSoapObject;
            localSoapSerializationEnvelope.dotNet = true;
            localSoapSerializationEnvelope.setOutputSoapObject(localSoapObject);
            new HttpTransportSE(
                    "http://218.90.160.85:10086/BusTravelGuideWebService/bustravelguide.asmx?wsdl")
                    .call("http://tempuri.org/" + soapAction, localSoapSerializationEnvelope);
            if (localSoapSerializationEnvelope.getResponse() == null
                    || localSoapSerializationEnvelope.bodyIn == null)
            {
                return null;
            }
            
            localSoapObject2 = (SoapObject) localSoapSerializationEnvelope.bodyIn;
        } 
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
            localSoapObject2 = null;
        } 
        catch (XmlPullParserException localXmlPullParserException)
        {
            localXmlPullParserException.printStackTrace();
        }

        return localSoapObject2;
    }
    
    public static SoapObject getSoapObject(String soapAction, HashMap<String, String> localHashMap)
    {                
        Util.log("SoapAction : " + soapAction);
        
        StringBuffer sff = new StringBuffer();
        sff.append("Input parameter:");
        if(localHashMap == null)
        {
            sff.append("\n").append("empty");
            localHashMap = new HashMap<String, String>();
        }
        else
        {
            for(Iterator<String> it =  localHashMap.keySet().iterator(); it.hasNext(); )   
            { 
                String key = (String)it.next();
                sff.append("\n").append(key + " = " + localHashMap.get(key));
            }
        }
        Util.log(sff.toString());        

        
        SoapObject localSoapObject = SOAPUtil.TransportData(soapAction,localHashMap);
        if(localSoapObject == null)
        {
            Util.log("localSoapObject is null");
            return null;
        }
        
        int propCnt = localSoapObject.getPropertyCount();
        for(int i = 0; i < propCnt; i++)
        {
            Util.log("SoapObject[" + i + "]" + localSoapObject.getProperty(i).toString());
        }
        
        return localSoapObject;
    }
}